package com.oauth2.authorization.service;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    static List<UserDetails>  userDetails;
    static {

    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        this.init();
        if (username == null) {
            return null;
        }
        for (UserDetails user : userDetails) {
            if (username.equals(user.getUsername())) {
                return user;
            }
        }
        return null;
    }

    private void init() {
        userDetails = Arrays.asList(User.withUsername("user1")
                        .password("123")
                        .roles("USER")
                        .build(),
                User.withUsername("admin")
                        .password("123")
                        .roles("USER")
                        .build());
    }
}
